﻿//========================================================= 
/**@file process_win_count.h
 * @brief 进程标题数监控项目
 * 
 * @date 2018-09-17   08:28:26
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_PROCESS_WIN_COUNT_H_
#define _LIBZHOUYB_PROCESS_WIN_COUNT_H_
//--------------------------------------------------------- 
#include "../../extension/ability/win_helper/WinHelper.h"
using zhou_yb::extension::ability::WinHelper;

#include "../../extension/ability/FolderHelper.h"
using zhou_yb::extension::ability::FileHelper;

#include "../rapidjson/rapidjson_extrator.h"
using zhou_yb::wrapper::rapidjson_extractor::RapidJsonArgParser;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace wrapper {
namespace zabbix_extractor {
//--------------------------------------------------------- 
/**
 * @brief 返回指定进程名称的窗口标题数
 * @date 2018-09-17 08:32
 */
class process_win_count_monitor
{
protected:
    static int put_err(int err)
    {
        return (-err);
    }
public:
    typedef int value_type;

    static size_t execute(const char* processName, const char* caption)
    {
        list_t<HWND> hWnds;
        return WinHelper::EnumHwnd(processName, caption, hWnds);
    }
    static int execute(const char* cfg, size_t& count)
    {
        string sJson = FileHelper::ReadToEnd(cfg);
        if(sJson.length() < 1)
            return put_err(DeviceError::ArgFormatErr);
        StringArguments arg;
        RapidJsonArgParser parser;
        parser.Parse(ByteArray(sJson.c_str(), sJson.length()), arg);
        string processName;
        if(!arg.Get("Process", processName))
            return DeviceError::ArgIsNullErr;
        string caption = arg["Caption"].To<string>("");
        count = execute(processName.c_str(), caption.c_str());
        return DeviceError::Success;
    }
    static value_type execute(int argc, const char* argv[])
    {
        if(argc < 2)
            return put_err(DeviceError::ArgErr);
        size_t count = execute(argv[1], argv[2]);
        return static_cast<value_type>(count);
    }
};
//--------------------------------------------------------- 
} // namespace zabbix_extractor 
} // namespace wrapper
} // namespace zhou_yb 
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_PROCESS_WIN_COUNT_H_
//========================================================= 